﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SimpleDatabaseAdapter
{
    public class ListData
    {
        private DataRow _row;
        private ILists _Parent;

        public ListData()
        {
        }
        internal ListData(DataRow row, ILists parent)
        {
            _row = row;
            _Parent = parent;
        }

        public int Id
        {
            get
            {
                if (_Id == int.MinValue)
                {
                    if (_row != null)
                    {
                        if (!DataTableUtils.TryGetFieldValueInt(_row[ListColumns.COLUMN_ID], out _Id))
                        {
                            _Id = -1;
                        }
                    }
                }
                return _Id;
            }
        }
        private int _Id = int.MinValue;

        public string Title
        {
            get
            {
                if (string.IsNullOrEmpty(_Title))
                {
                    if (_row != null)
                    {
                        _Title = DataTableUtils.GetFieldValue(_row[ListColumns.COLUMN_TITLE]);
                    }
                    else
                    {
                        _Title = String.Empty;
                    }
                }
                return _Title;
            }
            set
            {
                _Title = value;
            }
        }
        private string _Title;

        public string StaticName
        {
            get
            {
                if (string.IsNullOrEmpty(_StaticName))
                {
                    if (_row != null)
                    {
                        _StaticName = DataTableUtils.GetFieldValue(_row[ListColumns.COLUMN_STATIC_NAME]);
                    }
                    else
                    {
                        _StaticName = String.Empty;
                    }
                }
                return _StaticName;
            }
            set
            {
                _StaticName = value;
            }
        }
        private string _StaticName;

        public string Columns
        {
            get
            {
                if (string.IsNullOrEmpty(_Columns))
                {
                    if (_row != null)
                    {
                        _Columns = DataTableUtils.GetFieldValue(_row[ListColumns.COLUMN_COLUMNS]);
                    }
                    else
                    {
                        _Columns = GetEmptyColumns();
                    }
                }
                return _Columns;
            }
            set
            {
                _Columns = value;
            }
        }
        private string _Columns;

        private string GetEmptyColumns()
        {
            List<ListColumnBase> columnsData = new List<ListColumnBase>();
            string xml = Serializer.GetXML(columnsData);
            return xml;
        }

        public List<ListColumnBase> GetColumnsData()
        {
            List<ListColumnBase> _ColumnsData = new List<ListColumnBase>();
            try
            {
                _ColumnsData = (List<ListColumnBase>)Serializer.GetObject(Columns, _ColumnsData.GetType());
            }
            catch (Exception)
            {
            }
            return _ColumnsData;
        }
        public void AddColumn(ListColumnBase newColumn)
        {
            if (AllDataColumns.IsSystemColumn(newColumn.StaticName))
            {
                throw new Exception(string.Format("Static column name '{0}' is system.", newColumn.StaticName));
            }

            List<ListColumnBase> columnsData = GetColumnsData();

            if (columnsData.Where(x => x.StaticName == newColumn.StaticName).Count() == 0)
            {
                newColumn.AllDataColumnName = AllDataColumns.GetAllDataColumnName(columnsData, newColumn);
                columnsData.Add(newColumn);

                string xml = Serializer.GetXML(columnsData);
                Columns = xml;
            }
        }
        public void DeleteColumn(ListColumnBase column)
        {
            DeleteColumn(column.StaticName);
        }
        public void DeleteColumn(string staticName)
        {
            List<ListColumnBase> columnsData = GetColumnsData();
            var columns = columnsData.Where(x => x.StaticName == staticName);

            if (columns.Count() > 0)
            {
                columnsData.Remove(columns.First());

                string xml = Serializer.GetXML(columnsData);
                Columns = xml;
            }
        }

        public void UpdateList()
        {
            if (_Parent == null)
            {
                throw new Exception("Item not attach to lists.");
            }
            _Parent.UpdateList(this);
        }

        public T GetNewListItem<T>()
        {
            T item = (T)AllData.GetNewListItemStatic<T>(Id);
            return item;
        }
    }
}
